package com.ruoyi.workbench.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.workbench.domain.WorkReport;
import com.ruoyi.workbench.mapper.WorkReportMapper;
import com.ruoyi.workbench.service.WorkReportService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 工作报告(WorkReport)表服务实现类
 *
 * @author makejava
 * @since 2024-07-09 16:30:29
 */
@Service("workReportService")
public class WorkReportServiceImpl implements WorkReportService {
    @Resource
    private WorkReportMapper workReportDao;

    /**
     * 通过ID查询单条数据
     *
     * @param sid 主键
     * @return 实例对象
     */
    @Override
    public WorkReport queryById(Integer sid) {
        return this.workReportDao.queryById(sid);
    }

    /**
     * 分页查询
     *
     * @param workReport  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<WorkReport> queryByPage(WorkReport workReport, PageRequest pageRequest) {
        long total = this.workReportDao.count(workReport);
        return new PageImpl<>(this.workReportDao.queryAllByLimit(workReport, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param workReport 实例对象
     * @return 实例对象
     */
    @Override
    public WorkReport insert(WorkReport workReport) {
        this.workReportDao.insert(workReport);
        return workReport;
    }

    /**
     * 修改数据
     *
     * @param workReport 实例对象
     * @return 实例对象
     */
    @Override
    public WorkReport update(WorkReport workReport) {
        this.workReportDao.update(workReport);
        return this.queryById(workReport.getSid());
    }

    /**
     * 通过主键删除数据
     *
     * @param sid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer sid) {
        return this.workReportDao.deleteById(sid) > 0;
    }

    @Override
    public AjaxResult saveWorkReport(WorkReport workReport) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        workReport.setCompileId(Integer.valueOf(String.valueOf(user.getUserId())));
        workReport.setCreateTime(new Date());
        int insert = workReportDao.insertAll(workReport);
        return AjaxResult.success(workReport);
    }

    @Override
    public AjaxResult getWorkReportInfo(Integer sid) {
        return AjaxResult.success(workReportDao.queryById(sid));
    }

    @Override
    public AjaxResult getWorkReportByMatterId(Integer matterId) {
        List<WorkReport> workReports = workReportDao.selectAllByMatterId(matterId);
        return AjaxResult.success(workReports);
    }

    @Override
    public AjaxResult delWorkReportInfo(Integer sid) {
        int i = workReportDao.deleteById(sid);
        return AjaxResult.success(i);
    }
}
